package com.hxkj.service.impl;

import com.github.pagehelper.PageInfo;
import com.hxkj.client.service.ArticleServiceClient;
import com.hxkj.client.service.TagsServiceClient;
import com.hxkj.domain.enums.ArticleExceptionEnum;
import com.hxkj.domain.enums.TagsExceptionEnum;
import com.hxkj.domain.po.Article;
import com.hxkj.domain.po.Tags;
import com.hxkj.service.ArticleService;
import com.platform.common.domain.exception.CustomException;
import com.platform.common.domain.result.ParameterDataStruct;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultPageStruct;
import com.platform.common.domain.to.PageTo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
@Slf4j
public class ArticleServiceImpl implements ArticleService{

    @Autowired
    private ArticleServiceClient articleServiceClient;

    @Autowired
    private TagsServiceClient tagsServiceClient;

    //实时更新点击率  zhengyi
    @Override
    public Article modifyHeat(Article article) throws Exception {
        if (article.getSiteId() == null) {
            throw new CustomException(ArticleExceptionEnum.ARTICLE_SITEID_NULL);
        }
        if (article.getArticleId() == null) {
            throw new CustomException(ArticleExceptionEnum.ARTICLE_ID_NULL);
        }
        try {
            ResultDataStruct<Article> resultDataStruct;
            resultDataStruct = articleServiceClient.modifyHeat(article);
            if (resultDataStruct.getCode() != 200) {
                throw new CustomException(ArticleExceptionEnum.getEnumByCode(resultDataStruct.getCode()));
            }

            article = resultDataStruct.getData();
            return article;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public ResultPageStruct<Tags> getHotTagsList(Tags tags, PageTo pageParam) throws Exception {
        if (tags.getSiteId() == null) {
            throw new CustomException(TagsExceptionEnum.TAGS_NOT_SITEID);
        }
        ParameterDataStruct<Tags> parameterDataStruct = new ParameterDataStruct<>();
        parameterDataStruct.setPageTo(pageParam);
        parameterDataStruct.setData(tags);
        ResultPageStruct<Tags> resultPageStruct = new ResultPageStruct<>();
        try {
            resultPageStruct = tagsServiceClient.getHotTagsList(parameterDataStruct);
            resultPageStruct.getPageInfo().setCondition(tags.getSort());//将标签定义的排序规则封装返回
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultPageStruct;
    }

    @Override
    public PageInfo<Article> getNewestArticle(Long siteId, PageTo pageParams) throws Exception {
        //分页
        if(null == pageParams){
            pageParams = new PageTo();
            pageParams.setPageNum(1);
            pageParams.setPageSize(6);
        }
        if(Objects.isNull(pageParams.getPageNum())){
            pageParams.setPageNum(1);
        }
        if(Objects.isNull(pageParams.getPageSize())){
            pageParams.setPageSize(6);
        }
        //过滤参数
        Article articleParams = new Article();
        articleParams.setDelTag(0);
        articleParams.setAuditType(0);
        if(Objects.nonNull(siteId)){
            articleParams.setSiteId(siteId);
        }
        //查询
        ParameterDataStruct<Article> parameterDataStruct = new ParameterDataStruct<>();
        parameterDataStruct.setData(articleParams);
        parameterDataStruct.setPageTo(pageParams);
        PageInfo<Article> resultPageStruct = null;//articleServiceClient.getAllArticleList(parameterDataStruct);
        return resultPageStruct;
    }
}
